package au.com.iglootech.TaxTimeToolTip.ui.payment;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.Invoice;
import au.com.iglootech.TaxTimeToolTip.Entity.Task;
import au.com.iglootech.TaxTimeToolTip.component.InvoiceTable;
import au.com.iglootech.TaxTimeToolTip.dao.GenericDao;
import au.com.iglootech.TaxTimeToolTip.service.CompanyService;
import au.com.iglootech.TaxTimeToolTip.service.InvoiceTaxService;
import au.com.iglootech.TaxTimeToolTip.ui.invoice.InvoiceDateVO;
import au.com.iglootech.TaxTimeToolTip.ui.invoice.PerviewWindow;
import au.com.iglootech.TaxTimeToolTip.ui.invoice.PerviewWindow.DoAction;
import au.com.iglootech.TaxTimeToolTip.util.InvoiceUtil;
import au.com.iglootech.component.IGTTable;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Link;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;
@Configurable(preConstruction = true)
@Component
@Scope("request")
public class PayInvoiceTab extends CustomComponent {
	VerticalLayout mainLayout = new VerticalLayout();
	@Autowired
	private transient GenericDao genericDao;

	@Autowired
	private transient InvoiceTaxService invoiceTaxService;

	private BeanContainer<String, Invoice> beans;
	private ComboBox invoiceComboBox;
	private DateField startDate;
	private DateField endDate;
	private Set<Object> selectedItemIds = new HashSet<Object>();
	private Double totalGST;
	private TextArea detailsTextArea = new TextArea();
	private String selectedCaption;

	public PayInvoiceTab() {
		this.setCaption("Payment");
		beans = new BeanContainer<String, Invoice>(Invoice.class);
		beans.setBeanIdProperty("key");
		
		detailsTextArea.setWidth("500px");
		detailsTextArea.setHeight("80px");
		detailsTextArea.setCaption("Comment");
		initContent();
	}

	private void initContent() {
		Panel panel = new Panel();
		// HorizontalPanel searchBar = new HorizontalPanel();

		// company combobox
		List<Invoice> companies = invoiceTaxService.findUnPaidInvoice(null, null, null);
		invoiceComboBox = new ComboBox("Invoice Number");
		for (Invoice invoice : companies) {
			invoiceComboBox.addItem(invoice);
			invoiceComboBox.setItemCaption(invoice, InvoiceUtil.formatInvoiceNumber(invoice.getInvoiceNumber()));
		}

		invoiceComboBox.setImmediate(true);
		invoiceComboBox.setNullSelectionAllowed(false);
		invoiceComboBox.setFilteringMode(Filtering.FILTERINGMODE_STARTSWITH);
		invoiceComboBox.addListener(new Property.ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				selectedCaption = InvoiceUtil.formatInvoiceNumber(((Invoice)event.getProperty().getValue()).getInvoiceNumber());
			}
		});

		// search bar
		HorizontalLayout searchBar = new HorizontalLayout();
		searchBar.setSpacing(true);
		searchBar.addComponent(invoiceComboBox);

		Button searchButton = new Button("Search");
		searchButton.addListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent arg0) {
				refreshTabel();
			}
		});

		searchBar.addComponent(searchButton);
		searchBar.setComponentAlignment(searchButton, Alignment.BOTTOM_RIGHT);
		panel.addComponent(searchBar);
		
		InvoiceTable invoicetable = new InvoiceTable("Invoice List", beans);
		invoicetable.setWidth("100%");
		invoicetable.setHeight("500px");
		invoicetable.setColumnHeader("selected", "");
		invoicetable.addGeneratedColumn("selected", new Table.ColumnGenerator() {
			@Override
			public Object generateCell(Table source, final Object itemId,
					Object columnId) {
				boolean selected = selectedItemIds.contains(itemId);
				/*
				 * When the chekboc value changes, add/remove the itemId from
				 * the selectedItemIds set
				 */
				final CheckBox cb = new CheckBox("", selected);
				cb.addListener(new Property.ValueChangeListener() {
					@Override
					public void valueChange(Property.ValueChangeEvent event) {
						if (selectedItemIds.contains(itemId)) {
							selectedItemIds.remove(itemId);
						} else {
							selectedItemIds.add(itemId);
						}
						refreshTabel();
					}
				});
				return cb;
			}
		});
		invoicetable.setColumnHeader("Task Details", "Task Details");
		invoicetable.setColumnHeader("invoiceNumber", "Invoice Number");
		invoicetable.setColumnHeader("isPaid", "Paid");
		invoicetable.addGeneratedColumn("Task Details", new Table.ColumnGenerator() {
			
			@Override
			public Object generateCell(Table source, final Object itemId,
					Object columnId) {
				final Button detailsButton = new Button("Details");
				detailsButton.addStyleName("link");
				final Invoice invoice = beans.getItem(itemId).getBean();
				detailsButton.addListener(new Button.ClickListener() {					
					@Override
					public void buttonClick(ClickEvent arg0) {
						InvoiceDetailsWindow window = new InvoiceDetailsWindow(invoice);
						getWindow().addWindow(window);
					}
				});
				return detailsButton;
			}
		});
		invoicetable.setVisibleColumns(new String[] { "selected", "invoiceNumber",
				"isPaid", "details", "Task Details"});
		Button previewButton = new Button("Pay");
		previewButton.setWidth("120px");
		previewButton.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				List<Invoice> selectedInvoice = new ArrayList<Invoice>();
				for(Object itemId : selectedItemIds)
				{
					selectedInvoice.add(beans.getItem(itemId).getBean());
				}
				invoiceTaxService.payInvoice(selectedInvoice);
				getApplication().getMainWindow().showNotification("Your invoices has been changed.");
			}
		});
		panel.addComponent(invoicetable);
		// description
		HorizontalLayout optionBar = new HorizontalLayout();
		optionBar.setSpacing(true);
		optionBar.addComponent(detailsTextArea);
		optionBar.addComponent(previewButton);
		optionBar.setComponentAlignment(previewButton, Alignment.BOTTOM_RIGHT);
		panel.addComponent(optionBar);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}

	public InvoiceDateVO generateInvoiceDate(Company company, Double totalGST) {
		return new InvoiceDateVO(company, totalGST,
				(String) detailsTextArea.getValue());
	}


	private void refreshTabel() {
		invoiceComboBox.getValue();
		List<Invoice> result = invoiceTaxService.findUnPaidInvoice(selectedCaption, null, null);
		beans.removeAllItems();
		for (Invoice task : result) {
			beans.addBean(task);
		}
	}
}